<template>
  <div class="phone-management">
    <!-- 搜索|新增 -->
    <div class="set-parameters">
      <el-input
        v-model="wechatid"
        class="device-wechat-id"
        size="small"
        placeholder="请输入wxid"
        clearable
        minlength="6"
        maxlength="30"
        show-word-limit
      ></el-input>
      <el-input
        v-model="friendid"
        class="wechat-id-input device-wechat-id"
        size="small"
        placeholder="请输入好友的wxid"
        clearable
        minlength="6"
        maxlength="30"
        show-word-limit
      ></el-input>
      <el-select v-model="contenttype" size="small" placeholder="请选择消息类型" filterable clearable>
        <el-option v-for="cts in contentTypeState" :key="cts.id" :label="cts.label" :value="cts.value"></el-option>
      </el-select>
      <el-select v-model="type" size="small" placeholder="群聊" filterable clearable>
        <el-option v-for="ts in typeState" :key="ts.id" :label="ts.label" :value="ts.value"></el-option>
      </el-select>
      <el-select v-model="issend" size="small" placeholder="发送状态" filterable clearable>
        <el-option v-for="ss in sendState" :key="ss.id" :label="ss.label" :value="ss.value"></el-option>
      </el-select>
      <el-button type="primary" size="small" @click="queryData">查询</el-button>
    </div>

    <!-- 信息展示 -->
    <el-table :data="tableData" stripe style="width: 100%">
      <!-- <el-table-column prop="id" label="消息id" header-align="center" align="center"></el-table-column> -->
      <!-- <el-table-column prop="msgsvrid" label="内部消息id" header-align="center" align="center"></el-table-column> -->
      <el-table-column prop="wechatid" label="微信id" header-align="center" align="center">
        <template slot-scope="scope">
          <span v-text="scope.row.wechatid"></span>
        </template>
      </el-table-column>
      <el-table-column prop="friendid" label="好友id" header-align="center" align="center"></el-table-column>
      <el-table-column prop="contenttype" label="消息类型" header-align="center" align="center">
        <template slot-scope="scope">
          <span v-text="contentTypeMap[scope.row.contenttype] || scope.row.contenttype"></span>
        </template>
      </el-table-column>
      <el-table-column prop="content" label="内容" header-align="center" align="center">
        <template slot-scope="scope">
          <span :title="scope.row.content" class="message-content" v-text="scope.row.content"></span>
        </template>
      </el-table-column>

      <el-table-column label="群聊" header-align="center" align="center" width="100px">
        <template slot-scope="scope">
          <span v-if="scope.row.type === 1" style="color: #e6a23c" v-text="'群消息'"></span>
          <span v-else style="color: #67c23a" v-text="'个人消息'"></span>
        </template>
      </el-table-column>
      <el-table-column label="发送消息" header-align="center" align="center" width="100px">
        <template slot-scope="scope">
          <span v-if="scope.row.issend === 'true'" style="color: #67c23a" v-text="'发送的消息'"></span>
          <span v-else style="color: #e6a23c" v-text="'收到的消息'"></span>
        </template>
      </el-table-column>
      <el-table-column label="消息时间" header-align="center" align="center">
        <template slot-scope="scope">
          <span v-text="$options.filters.transformTime2Minute(scope.row.createTime)"></span>
        </template>
      </el-table-column>
      <el-table-column label="操作" header-align="center" align="center" width="80px">
        <template slot-scope="scope">
          <el-button size="mini" style="background-color: #ff9f00; color: #ffffff" type="warning" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      :current-page="pageNo"
      :page-sizes="[20, 40, 60, 80]"
      :page-size="pageSize"
      :pager-count="5"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    ></el-pagination>
  </div>
</template>

<script>

import { mapState } from 'vuex'
import { queryMessages, deleteMessages } from '@/api/httpApiAdmin'

export default {
  name: 'MessageLog',
  data() {
    return {
      tableData: [], // 表格中的数据
      wechatid: '', // 微信id
      friendid: '',
      issend: '', // 发送的消息还是收到的消息
      sendState: [
        {
          id: 'issend0',
          value: 'false',
          label: '收到的消息'
        }, {
          id: 'issend1',
          value: 'true',
          label: '发送的消息'
        }
      ], // 消息发送状态
      contenttype: '', // 消息类型
      contentTypeState: [
        {
          id: 'contentTypeState0',
          value: 0,
          label: '未知内容'
        }, {
          id: 'contentTypeState1',
          value: 1,
          label: '文本内容'
        }, {
          id: 'contentTypeState2',
          value: 2,
          label: '图片消息'
        }, {
          id: 'contentTypeState3',
          value: 3,
          label: '语音消息'
        }, {
          id: 'contentTypeState4',
          value: 4,
          label: '视频消息'
        }, {
          id: 'contentTypeState5',
          value: 5,
          label: '系统消息'
        }, {
          id: 'contentTypeState6',
          value: 6,
          label: '链接消息'
        }, {
          id: 'contentTypeState7',
          value: 7,
          label: '扩展的链接消息'
        }, {
          id: 'contentTypeState8',
          value: 8,
          label: '文件发送'
        }, {
          id: 'contentTypeState9',
          value: 9,
          label: '名片'
        }, {
          id: 'contentTypeState10',
          value: 10,
          label: '位置信息'
        }, {
          id: 'contentTypeState11',
          value: 11,
          label: '红包'
        }, {
          id: 'contentTypeState12',
          value: 12,
          label: '转账'
        }, {
          id: 'contentTypeState13',
          value: 13,
          label: '小程序'
        }, {
          id: 'contentTypeState14',
          value: 14,
          label: 'Emoji'
        }, {
          id: 'contentTypeState15',
          value: 15,
          label: '群管理消息'
        }, {
          id: 'contentTypeState16',
          value: 16,
          label: '领取红包消息'
        }, {
          id: 'contentTypeState17',
          value: 17,
          label: '群聊系统消息'
        }, {
          id: 'contentTypeState18',
          value: 18,
          label: '公众号文章'
        }, {
          id: 'contentTypeState19',
          value: 19,
          label: '语音通话'
        }, {
          id: 'contentTypeState20',
          value: 20,
          label: '视频通话'
        }, {
          id: 'contentTypeState21',
          value: 21,
          label: '服务通知'
        }, {
          id: 'contentTypeState22',
          value: 22,
          label: '引用通知'
        }, {
          id: 'contentTypeState99',
          value: 99,
          label: '不支持的消息'
        }
      ], // 消息类型
      contentTypeMap: {
        0: '未知内容',
        1: '文本内容',
        2: '图片消息',
        3: '语音消息',
        4: '视频消息',
        5: '系统消息',
        6: '链接消息',
        7: '扩展的链接消息',
        8: '文件发送',
        9: '名片',
        10: '位置信息',
        11: '红包',
        12: '转账',
        13: '小程序',
        14: 'Emoji',
        15: '群管理消息',
        16: '领取红包消息',
        17: '群聊系统消息',
        18: '公众号文章',
        19: '语音通话',
        20: '视频通话',
        21: '服务通知',
        22: '引用通知',
        99: '不支持的消息'
      }, // 消息类型对照表
      type: '', // 0聊天消息  1群消息
      typeState: [
        {
          id: 'typeState0',
          value: 0,
          label: '聊天消息'
        }, {
          id: 'typeState1',
          value: 1,
          label: '群消息'
        }
      ], // 群聊还是个人聊天

      pageNo: 1, // 第几页
      pageSize: 20, // 每页几个数据
      total: 0 // 总数
    }
  },
  computed: {
    ...mapState({
      adminInfo: 'adminInfo'
    })
  },
  mounted() {
    this.queryDataSilence()
  },
  methods: {
    // 分页查询数据
    queryData() {
      queryMessages(this.adminInfo.cid, this.wechatid, this.friendid, this.issend, this.contenttype, this.type, this.pageNo, this.pageSize)
        .then((res) => {
          if (res.code === 0) {
            this.tableData = res.data.rows || []
            this.total = res.data.records || 0
            this.$message({
              type: 'success',
              message: '查询成功！',
              duration: 1000
            })
          }
        })
    },
    queryDataSilence() {
      queryMessages(this.adminInfo.cid, this.wechatid, this.friendid, this.issend, this.contenttype, this.type, this.pageNo, this.pageSize)
        .then((res) => {
          if (res.code === 0) {
            this.tableData = res.data.rows || []
            this.total = res.data.records || 0
          }
        })
    },
    // 修改pageSize
    handleSizeChange(val) {
      if (this.pageSize === val) return
      this.pageSize = val
      this.queryData()
    },
    // 修改当前页
    handleCurrentChange(val) {
      this.pageNo = val
      this.queryData()
    },
    // 删除
    handleDelete(index, row) {
      this.$confirm('是否删除设备？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteMessages(row.id).then((res) => {
          if (res.code === 0) {
            const pn = Math.ceil((this.total[this.activeName] - 1) / this.pageSize)
            if (this.pageNo > pn) {
              this.pageNo = pn
            }
            this.queryDataSilence()
            this.$message({
              type: 'success',
              message: '删除成功!',
              duration: 1000
            })
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消',
          duration: 1000
        })
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.phone-management {
  height: 100%;
  display: flex;
  flex-direction: column;

  .set-parameters {
    padding: 10px 5px;
    display: flex;
    align-items: center;
    border-bottom: solid $border-color-1 2px;
    .el-button,
    .wechat-id-input,
    .el-select {
      margin-left: 20px;
    }
    ::v-deep .device-wechat-id {
      width: 220px;
      .el-input__inner {
        width: 220px;
      }
    }
  }

  .el-pagination {
    padding: 5px;
    display: flex;
    justify-content: center;
    align-items: center;
    border-top: solid 1px $border-color-1;
    background-color: white;
  }

  ::v-deep .message-content {
    @include ellipsis(1);
  }
}
</style>
